package com.wan.api.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wan.entity.GameChatContent;
import com.wan.entity.GameUser;
import com.wan.service.GameChatContentService;
import com.wan.service.GameUserService;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author xzx
 * @date 2021/4/15 13:50
 */
@RestController
@RequestMapping("/bs/game")
public class GameChatController {
    @Autowired
    private GameChatContentService gameChatContentService;
    @Autowired
    private GameUserService gameUserService;


    /**
     * 好友聊天
     * @param str
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @RequestMapping("/send")
    public ResultBean sendMessage(@RequestBody String str) throws IOException, InterruptedException {
        JSONObject jsonObject = JSON.parseObject(str);
        GameChatContent chatContent = new GameChatContent();
        chatContent.setSelfId(jsonObject.getInteger("selfId"));
        chatContent.setFriendId(jsonObject.getInteger("friendId"));
        chatContent.setContent(jsonObject.getString("content"));
        chatContent.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        gameChatContentService.save(chatContent);
        GameUser user = gameUserService.getById(chatContent.getFriendId());



        String content = this.getContent(chatContent.getContent());
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("friendId",user.getId());
        jsonObject1.put("friendImg",user.getFaceImg());
        jsonObject1.put("content",content);
        jsonObject1.put("time",DateUtil.format(new Date(),"yyyy/MM/dd HH:mm:ss"));
//        WebSocketServer.sendInfo(JSON.toJSONString(jsonObject1),String.valueOf(chatContent.getSelfId()));
        return new ResultBean(200,"成功",jsonObject1);
    }


    private String getContent(String str){
        List<String> list0 = Arrays.asList("今天不想玩，要去打球 拜~","上号 go  游戏见","我压缩贼 6 上号",
                "你太菜了，不想玩，再见","晚上玩一会吧 我要去看电影了","晚上一起 排位吧 今天上王者 晚上见",
                "今天没空","盲僧打野带你飞 游戏见","小学僧 不跟你玩","我是托儿索, 游戏见");


        List<String> list1 = Arrays.asList("哈哈，在的","在的 怎么了？","在的，不过我要去吃饭了","在忙 一会说","好的 我知道了 在忙一会说");
        List<String> list2 = Arrays.asList("在忙 一会说","好的 我知道了 在忙一会说");

        if (str.contains("玩") || str.contains("玩游戏")){
            int number = new Random().nextInt(10) + 1;
            return list0.get(number);
        } else if (str.equals("你好")){
            return "你好";
        } else if (str.contains("在")){
            int number = new Random().nextInt(4) + 1;
            return list1.get(number);
        } else {
            int number = new Random().nextInt(1) + 1;
            return list2.get(number);
        }
    }


}
